﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class HocKyListView : Form
    {
        private BindingList<HocKy> bindingListHocKy;
        private HocKy _hocKy;

        public HocKyListView()
        {
            InitializeComponent();
        }

        private void HocKyListView_Load(object sender, EventArgs e)
        {
            string[] listFieldName = new string[] { "MaQuanLy", "TenHocKy", "NamHoc.TenNamHoc", "TuNgay", "DenNgay" };
            string[] listColumnName = new string[] { "Mã Quản Lý", "Tên Học Kỳ", "Năm Học", "Từ Ngày", "Đến Ngày" };
            AppGridView.InitGridView(gvHocKy, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, true);
            AppGridView.ReadOnlyGridView(gvHocKy);
            AppGridView.ShowField(gvHocKy, listFieldName, listColumnName);

            LoadData();
        }

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _hocKy = new HocKy();
            OpenDetailForm();
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteRow();
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnLamMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }
        private void gcHocKy_DoubleClick(object sender, EventArgs e)
        {
            _hocKy = gvHocKy.GetFocusedRow() as HocKy;
            if (_hocKy != null)
                OpenDetailForm();
        }
        #region User functions
        void LoadData()
        {
            List<HocKy> list = DataAccessObject.HocKy.GetListHocKy();
            bindingListHocKy = new BindingList<HocKy>(list);

            bindingSourceHocKy.DataSource = bindingListHocKy;
            gvHocKy.Invalidate();
        }
        void OpenDetailForm()
        {
            HocKyDetailView detailView = new HocKyDetailView(_hocKy);
            detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
            detailView.DeleteChanged += new EventHandler(detailView_DeleteChanged);
            detailView.ShowDialog();
        }

        void detailView_DeleteChanged(object sender, EventArgs e)
        {
                DeleteRow();
        }

        void detailView_SaveChanged(object sender, EventArgs e)
        {
            bindingListHocKy.Add(_hocKy);
            gvHocKy.Invalidate();
        }
        private void DeleteRow()
        {
            if (MessageBox.Show("Bạn có chắc xóa?", "Xóa một dòng thông tin học kỳ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _hocKy = gvHocKy.GetFocusedRow() as HocKy;

                if (_hocKy != null && _hocKy.Oid > 0 && DataAccessObject.HocKy.Delete(_hocKy) > 0)
                {
                    gvHocKy.DeleteSelectedRows();
                    gvHocKy.Invalidate();
                }
                else
                    MessageBox.Show("Không xóa được", "Có Lỗi Xảy Ra", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion


    }
}
